% note here we only plot the case where the agent does not adjust
clear all;
cd ..; cd ..; cd ..;
load('code_main_model\6_implied_w_r\computation_results\data.mat','adjust_noad_ini',...
    'nb','ntheta','nz','gridb','gridtheta','gridz','nb_pdf','ntheta_pdf','gridb_pdf','gridtheta_pdf');
cd 'code_figure_table\OA\Figure OA5';

farmarketindex=2;
closemarketindex=1;

ratio=0.8;
[~,index]=min(abs(gridtheta-ratio));

figure('color',[1,1,1],'Position',[100,100,400,320]);

%% far market
occupation_choice=squeeze(adjust_noad_ini(farmarketindex,:,:,index));
worker_entre_boundary(1:nz)=0.0;
entre_credit_lower_boundary(1:nz)=0.0;
entre_credit_upper_boundary(1:nz)=0.0;

for i_z=1:nz
    index2=find(occupation_choice(i_z,:)==2);
    index3=find(occupation_choice(i_z,:)==3);
    if (isempty(index3) && isempty(index2))
        worker_entre_boundary(i_z)=nb;
        entre_credit_lower_boundary(i_z)=nb;
        entre_credit_upper_boundary(i_z)=nb;
    elseif (isempty(index3) && ~isempty(index2))
        worker_entre_boundary(i_z)=min(index2);
        entre_credit_lower_boundary(i_z)=nb;
        entre_credit_upper_boundary(i_z)=nb; 
    elseif (~isempty(index3) && isempty(index2))
        worker_entre_boundary(i_z)=min(index3);
        entre_credit_lower_boundary(i_z)=min(index3);
        entre_credit_upper_boundary(i_z)=max(index3);
    else
        worker_entre_boundary(i_z)=min(min(index2),min(index3));
        entre_credit_lower_boundary(i_z)=min(index3);
        entre_credit_upper_boundary(i_z)=max(index3);
    end
end

temp=find(entre_credit_lower_boundary<nb);
start_from=temp(1);

% remodify the entre_credit_lower_boundary and entre_credit_upper_boundary
x1=gridb(worker_entre_boundary);
y1=gridz;
x2=[gridb(flip(entre_credit_lower_boundary(start_from:nz))), gridb(entre_credit_upper_boundary(start_from:nz))];
y2=[flip(gridz(start_from:nz)), gridz(start_from:nz)];

axis square
plot(x1,y1,'-b','linewidth',2)
hold on
plot(x2,y2,'--b','linewidth',2)
% xlim([0,80]);
% ylim([1,3]);

%% near market
occupation_choice=squeeze(adjust_noad_ini(closemarketindex,:,:,index));
worker_entre_boundary(1:nz)=0.0;
entre_credit_lower_boundary(1:nz)=0.0;
entre_credit_upper_boundary(1:nz)=0.0;

for i_z=1:nz
    index2=find(occupation_choice(i_z,:)==2);
    index3=find(occupation_choice(i_z,:)==3);
    if (isempty(index3) && isempty(index2))
        worker_entre_boundary(i_z)=nb;
        entre_credit_lower_boundary(i_z)=nb;
        entre_credit_upper_boundary(i_z)=nb;
    elseif (isempty(index3) && ~isempty(index2))
        worker_entre_boundary(i_z)=min(index2);
        entre_credit_lower_boundary(i_z)=nb;
        entre_credit_upper_boundary(i_z)=nb;
    elseif (~isempty(index3) && isempty(index2))
        worker_entre_boundary(i_z)=min(index3);
        entre_credit_lower_boundary(i_z)=min(index3);
        entre_credit_upper_boundary(i_z)=max(index3);
    else
        worker_entre_boundary(i_z)=min(min(index2),min(index3));
        entre_credit_lower_boundary(i_z)=min(index3);
        entre_credit_upper_boundary(i_z)=max(index3);
    end
end

temp=find(entre_credit_lower_boundary<nb);
start_from=temp(1);

% remodify the entre_credit_lower_boundary and entre_credit_upper_boundary
x3=gridb(worker_entre_boundary);
y3=gridz;
x4=[gridb(flip(entre_credit_lower_boundary(start_from:nz))), gridb(entre_credit_upper_boundary(start_from:nz))];
y4=[flip(gridz(start_from:nz)), gridz(start_from:nz)];

plot(x3,y3,':r','linewidth',2)
hold on
plot(x4,y4,':r','linewidth',2)
xlim([0,20]);
ylim([1,2.5]);
xlabel('wealth')
ylabel('talent')

save('wealth_prod.mat','x1','y1','x2','y2','x3','y3','x4','y4')